package com.hj.auth.api;

import com.hj.auth.model.request.TokenRequest;
import com.hj.auth.service.AuthServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Oauth2Controller {

    @Autowired
    private AuthServiceImpl authService;

    @RequestMapping("/access_token")
    public void accessToken(@RequestBody TokenRequest request){
        MultiValueMap<String, String> body = new LinkedMultiValueMap<>();
        body.add("grant_type","authorization_code");
        body.add("code",request.getCode());
        body.add("redirect_uri",request.getRedirectUri());
        authService.applyToken(body,request);
    }

    @RequestMapping("/refresh_token")
    public void refreshToken(@RequestBody TokenRequest request){
        MultiValueMap<String, String> body = new LinkedMultiValueMap<>();
        body.add("grant_type","refresh_token");
        body.add("refresh_token",request.getRefreshToken());
        authService.applyToken(body,request);
    }
}
